﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using vg;

namespace 电力系统可视化平台
{
    public partial class FormMain : Form
    {
        string exeName = "电力系统可视化平台";
        public Ivg mainVg;
        string nvgPath;
        string state;
        string sql;
        SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=DataBase;Integrated Security=True");

        public FormMain()
        {
            InitializeComponent();
        }
        private void dataBaseInitialize()
        {
            //初始化toolStripComboBox1
            conn.Open();//整个项目只在此打开一次数据库，不关闭，这样虽然不安全，但是反复打开关闭数据库会耗费大量时间

            if (conn.State == ConnectionState.Open)
            {
                sql = "select caseName from caseInfo";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    toolStripComboBox1.Items.Add(sdr[0].ToString());
                }
                //conn.Dispose();
                sdr.Close();
            }
            else 
            {
                MessageBox.Show("数据库打开失败，请检查数据库完整性及权限！");
            }
                
        }
        private void FormMain_Load_1(object sender, EventArgs e)
        {
            dataBaseInitialize();
            this.Text = exeName;
            //编辑模块初始化
            state = "浏览";
            editToolsIntialize();
            //以浏览模式默认打开
            mainVg = axvgctrl1.Run("");
            tableLayoutPanel3.RowStyles[0].Height = 0;//90
            tableLayoutPanel4.ColumnStyles[0].Width = 0;//180
            tableLayoutPanel4.ColumnStyles[2].Width = 0;//230

        }
        private void browse_Click(object sender, EventArgs e)
        {
            state = "浏览";
            mainVg = axvgctrl1.Run("");
            axvgctrl5.Run(axvgctrl1.DefaultPath + "btnview.tbl");
            axvgctrl5.vg.ActiveSheet.SetPropertyValue("cursheet", axvgctrl1.vg.ActiveSheet);
            axvgctrl5.vg.ActiveSheet.SetPropertyValue("cursheet", axvgctrl1.vg.ActiveSheet);
            if (axvgctrl5.vg != null)
            {
                if (axvgctrl5.vg.ActiveSheet != null)
                    axvgctrl5.vg.ActiveSheet.SetPropertyValue("CurSheet", axvgctrl1.vg.ActiveSheet);
            }
            //隐藏多余的控件
            tableLayoutPanel3.RowStyles[0].Height = 0;//90
            tableLayoutPanel4.ColumnStyles[0].Width = 0;//180
            tableLayoutPanel4.ColumnStyles[2].Width = 0;//230

            //浏览同样也具有打开功能
            openFilesFun();
        }

        private void edit_Click(object sender, EventArgs e)
        {
            state = "编辑";
            editToolsIntialize();
            tableLayoutPanel3.RowStyles[0].Height = 90;//90
            tableLayoutPanel4.ColumnStyles[0].Width = 180;//180
            tableLayoutPanel4.ColumnStyles[2].Width = 230;//230
            panel2.Visible = true;

        }

        #region 组建编辑模式下的各功能模块
        private void editToolsIntialize()
        {
            //模块初始化
            mainVg = axvgctrl1.Design("");
            var s = axvgctrl1.DefaultPath;
            axvgctrl4.Run(s + "inspector.tbl");
            axvgctrl2.Run(s + "libview.tbl");
            axvgctrl3.Run(s + "toolbar.tbl");
            axvgctrl5.Run(s + "btnview.tbl");
            axvgctrl5.vg.ActiveSheet.SetPropertyValue("cursheet", axvgctrl1.vg.ActiveSheet);
            axvgctrl3.vg.ActiveSheet.Execute("btnPaste.Enable=true");
            axvgctrl3.vg.ActiveSheet.SetPropertyValue("cursheet", axvgctrl1.vg.ActiveSheet);
            axvgctrl5.vg.ActiveSheet.SetPropertyValue("curSheet", axvgctrl1.vg.ActiveSheet);
            axvgctrl2.vg.ActiveSheet.SetPropertyValue("curSheet", axvgctrl1.vg.ActiveSheet);
            var g = axvgctrl1.vg.Selection;
            axvgctrl4.vg.ActiveSheet.SetPropertyValue("selected", g);
            axvgctrl3.vg.ActiveSheet.Execute("OnSelectChange()");
        }
        private void axvgctrl1_OnSheetChange(object sender, EventArgs e)
        {
            if (axvgctrl2.vg != null)
            {
                if (axvgctrl2.vg.ActiveSheet != null)
                {
                    axvgctrl2.vg.ActiveSheet.SetPropertyValue("cursheet", axvgctrl1.vg.ActiveSheet);
                }
            }
            if (axvgctrl3.vg != null)
            {
                if (axvgctrl3.vg.ActiveSheet != null)
                {
                    axvgctrl3.vg.ActiveSheet.SetPropertyValue("cursheet", axvgctrl1.vg.ActiveSheet);
                }
            }
            if (axvgctrl5.vg != null)
            {
                if (axvgctrl5.vg.ActiveSheet != null)
                    axvgctrl5.vg.ActiveSheet.SetPropertyValue("CurSheet", axvgctrl1.vg.ActiveSheet);
            }
        }

        private void axvgctrl1_OnModifyChange(object sender, EventArgs e)
        {
            if (axvgctrl3.vg != null)
            {
                if (axvgctrl3.vg.ActiveSheet != null)
                    axvgctrl3.vg.ActiveSheet.Execute("btnSave.Enable=CurSheet.Owner.Modified");
            }
        }

        private void axvgctrl1_OnSelectChange(object sender, EventArgs e)
        {
            axvgctrl4.vg.ActiveSheet.SetPropertyValue("selected", axvgctrl1.vg.Selection);
            axvgctrl3.vg.ActiveSheet.Execute("OnSelectChange()");
            axvgctrl3.vg.ActiveSheet.Execute("btnUndo.Enable=CurSheet.Owner.CanUndo()");
            axvgctrl3.vg.ActiveSheet.Execute("btnRedo.Enable=CurSheet.Owner.CanRedo()");
        }
        #endregion


        #region 工具栏各功能实现

        private void openFilesFun()
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                nvgPath = openFileDialog1.FileName;
                FileInfo fileinfo = new FileInfo(nvgPath);
                string type = Path.GetExtension(fileinfo.FullName);
                string fileName = fileinfo.Name;
                this.Text = exeName + "  " + nvgPath;
                //记录数据至数据库以及combox中
                //conn.Open();
                if (conn.State == ConnectionState.Open)
                {
                    //若重复打开了同一文件，则不向数据库中添加
                    sql = "select casePath from caseInfo where convert(nvarchar(255),casePath)='" + nvgPath + "'";//text与string 之间不能直接等于
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    SqlDataReader sdr = cmd.ExecuteReader();
                    if (!sdr.HasRows)
                    {
                        sdr.Close();
                        sql = "insert into caseInfo (caseName,casePath) values ('" + fileName + "','" + nvgPath + "')";
                        cmd = new SqlCommand(sql, conn);
                        cmd.ExecuteNonQuery();
                        toolStripComboBox1.Items.Add(fileName);
                    }
                    else
                    {
                        sdr.Close();
                    }
                    //conn.Dispose();

                    this.Text = exeName + "  " + nvgPath;
                    if (state == "浏览")
                    {
                        mainVg = axvgctrl1.Run(nvgPath);
                    }
                    if (state == "编辑")
                    {
                        mainVg = axvgctrl1.Design(nvgPath);
                    }

                    toolStripComboBox1.Items.Add(fileName);
                    axvgctrl1.vg.ZoomFit();//使svg能自动适应当前控件大小
                }
            }
        }
        private void openFile_Click(object sender, EventArgs e)
        {
            openFilesFun();
        }

        private void print_Click(object sender, EventArgs e)
        {
            if (mainVg != null)
            {
                axvgctrl1.vg.Print(true);
            }
        }

        private void zoomIn_Click(object sender, EventArgs e)
        {
            if (mainVg != null)
            {
                axvgctrl1.vg.ZoomIn();
            }
        }

        private void zoomOut_Click(object sender, EventArgs e)
        {
            if (mainVg != null)
            {
                axvgctrl1.vg.ZoomOut();
            }
        }

        private void fitPage_Click(object sender, EventArgs e)
        {
            if (mainVg != null)
            {
                axvgctrl1.vg.ZoomFit();
            }
        }

        private void move_Click(object sender, EventArgs e)
        {
            if (mainVg != null)
            {
                axvgctrl1.vg.MoveSheet();
            }
        }
        #endregion

        private void toolStripComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = toolStripComboBox1.SelectedIndex;
            string selectedFilesPath = null;
            sql = "select casePath from caseInfo ";
            //SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=testDatabase;Integrated Security=True");
            //conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader sdr = cmd.ExecuteReader();//SqlDataReader只能返回一条数据
            int rowNum = 0;
            while (sdr.Read())
            {
                if (rowNum == id)
                {
                    selectedFilesPath = sdr[0].ToString();
                    break;
                }
                else
                {
                    rowNum++;
                }
            }
            if (state == "编辑")
            {
                mainVg = axvgctrl1.Design(selectedFilesPath);
            }
            if (state == "浏览")
            {
                mainVg = axvgctrl1.Run(selectedFilesPath);
            }
            this.Text = exeName + "  " + selectedFilesPath;
            axvgctrl1.vg.ZoomFit();
            //conn.Dispose();
            sdr.Close();
        }



    }
}
